/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVA-Data-Structures-and-Algorithms PROJECT
 * All Rights Reserved.
 */

package 二分;

/**
 * @Description 二分查找
 * 在有序数组中迅速地找到一个值
 * @Author 俊昭
 * @Date 2022/5/2
 */
public class Binary {
    private static int search(int[] arr, int num, int left, int right) {
        int mid = (left + right) / 2;

        while (left < right) {
            if (arr[mid] == num) {
                return mid;
            } else if (mid > num) {
                right = mid - 1;
                return search(arr, num, left, right);
            } else if (mid < num) {
                left = mid + 1;
                return search(arr, num, left, right);
            }
        }
        return - 1;
    }

    public static int search(int[] arr, int num) {
        return search(arr, num, 0, arr.length - 1);
    }
}
